package whf.framework.meta.service;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import whf.framework.exception.AppException;
import whf.framework.exception.CreateException;
import whf.framework.exception.DAOException;
import whf.framework.exception.DuplicateException;
import whf.framework.exception.FindException;
import whf.framework.exception.PropertyException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.exception.UpdateException;
import whf.framework.meta.dao.ViewDAO;
import whf.framework.meta.dao.ViewPropertyDAO;
import whf.framework.meta.entity.Object;
import whf.framework.meta.entity.View;
import whf.framework.meta.entity.ViewProperty;
import whf.framework.util.BeanFactory;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class ViewServiceImp extends whf.framework.service.ServiceImp<View> implements ViewService {

	private ViewDAO viewDAO;
	
	private ViewPropertyDAO viewPropertyDAO;

	public void setViewDAO(ViewDAO viewDAO) {
		this.viewDAO = viewDAO;
	}

	public void setViewPropertyDAO(ViewPropertyDAO viewPropertyDAO) {
		this.viewPropertyDAO = viewPropertyDAO;
	}

	public static ViewService getViewService() throws ServiceNotFoundException {
		return (ViewService)BeanFactory.getService(ViewServiceImp.class);
	}

	/* (non-Javadoc)
	 * @see whf.framework.service.ServiceImp#getBoClass()
	 */
	@Override
	public Class getEntityClass() {
		return View.class;
	}

	/* (non-Javadoc)
	 * @see whf.framework.service.FindService#findByCode(java.lang.String)
	 */
	public View findByCode(String code) throws FindException{
		List<View> list = super.find("t.code=?", code);
		if(list.size() > 0){
			return list.get(0);
		} else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ViewService#addProperty(whf.framework.meta.entity.View, whf.framework.meta.entity.ViewProperty)
	 */
	public void addProperty(View view, ViewProperty property) throws CreateException, UpdateException{
		if(property.getId() == 0){
			this.viewPropertyDAO.create(property);
		}
		view.addProperty(property);
		this.viewDAO.update(view);
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ViewService#createByObject(whf.framework.meta.entity.Object)
	 */
	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ViewService#createByObject(whf.framework.meta.entity.Object)
	 */
	public View createByObject(Object object) throws CreateException {
		try{
			View view = new View(object);
			this.viewDAO.create(view);
			return view;
		}catch(PropertyException e){
			throw new CreateException(e);
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.service.ServiceImp#duplicate(whf.framework.entity.IBaseBO, int)
	 */
	@Override
	public View duplicate(View bo, int deepth) throws DuplicateException, CreateException {
		return super.duplicate(bo, 1);
	}

	public void importFromXml(Document doc) throws DAOException {
		Element root = doc.getRootElement();
		List views = root.getChildren("view");
		for(int i=0; i< views.size(); i++){
			Element ve = (Element)views.get(i);
			String code = ve.getAttributeValue("code");
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.meta.service.ViewService#exportToXml(org.jdom.Document)
	 */
	public Document exportToXml(Document doc) throws AppException {
		return null;
	}
	
} 